package com.penghaisoft.microwms.base.web.expose.controller;

import com.alibaba.fastjson.JSONArray;
import com.penghaisoft.microwms.base.web.auth.service.IBaseFactoryService;
import com.penghaisoft.microwms.base.web.auth.service.IUserService;
import com.penghaisoft.microwms.base.web.data.service.IBaseDictionaryService;
import com.penghaisoft.microwms.base.web.data.service.IMdContractService;
import com.penghaisoft.microwms.base.web.data.service.IMdLocationService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.base.model.BaseFactory;
import com.penghaisoft.microwms.common.base.model.BaseUser;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdContract;
import com.penghaisoft.microwms.common.data.model.MdLocation;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.dto.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: micro-wms
 * @description: 免登录手持接口
 * @author: zhangx
 * @create: 2018-05-17 15:23
 **/
@RestController
@RequestMapping(value = "/expose/hand")
public class ExposeHandController extends BaseController {


    @Autowired
    private IUserService userService;

    @Autowired
    private IBaseFactoryService baseFactoryService;

    @Autowired
    private IMdLocationService locationService;
    
    @Autowired
	private IMdContractService mdContractService;
    
    @Autowired
	private IBaseDictionaryService baseDictionaryService;
    
    /**
     * 获取系统时间
     * @return
     */
    @PostMapping("systime")
    public HandResp systime() {
        HandResp result = new HandResp();
        result.setCode("1");
        result.setValue(Constant.sdfYMDHMS.format(new Date()));
        return result;
    }

    /**
     * 连接测试
     * @return
     */
    @PostMapping("connectiontest")
    public HandResp connectiontest() {
        HandResp result = new HandResp();
        result.setCode("1");
        return result;
    }

    /**
     * 手持用户信息
     * @param param
     * @return
     */
    @PostMapping("userinfo")
    public HandResp getUserInfo(@RequestBody BaseUser param) {
        HandResp result = new HandResp();
        BaseUser user = userService.queryOne(param);
        if (user!=null) {
            String userId = user.getUserId();
            List<BaseFactory> factories = baseFactoryService.queryUserFactory(userId);
            List<UserInfo> infos = new ArrayList<>();
            if (factories!=null&&factories.size()==1) {
                result.setCode(Constant.SUCCESS);
                BaseFactory factory = factories.get(0);
                UserInfo info = new UserInfo();
                info.setLoginName(user.getLoginName());
                info.setUserName(user.getUserName());
                info.setFactoryCode(factory.getFactoryCode());
                info.setFactoryName(factory.getFactoryName());
                infos.add(info);
//				5 该工厂是否允许手动输入
//                boolean allowHandInputFlag = allowHandInput(factory.getFactoryCode());
//                if (allowHandInputFlag) {
//                    result.setValue("1");
//                }else {
//                    result.setValue("0");
//                }
                result.setList(infos);
            }else {
                result.setCode(Constant.FAIL);
                result.setMsg("此用户多工厂！");
            }
        }else {
            result.setCode(Constant.FAIL);
            result.setMsg(param.getLoginName()+" 此账号不存在!");
        }
        return result;
    }

    /**
     * 获取用户登录工作区下的库位
     * @param param
     * @return
     */
    @PostMapping("locations")
    public HandResp getLocations(@RequestBody UserInfo param) {
        HandResp result = new HandResp();

//		1查询出用户的工作区
        String workAreaCode = param.getWorkareaCode();
//		2查询出每个工作区下的库位
        List<MdLocation> locations = new ArrayList<>();
        if (workAreaCode!=null) {
            MdLocation mdLocation = new MdLocation();
            mdLocation.setWorkArea(workAreaCode);
            locations.add(mdLocation);
            locations = locationService.queryAllUnderWorkArea(locations);
            List<String> locs  = new ArrayList<>();
            for (MdLocation location : locations) {
                locs.add(location.getLocationCode());
            }
            result.setValues(locs);
            result.setCode(Constant.SUCCESS);
        }else {
            result.setCode(Constant.FAIL);
            result.setMsg("请输入工作区编码！");
        }

        return result;
    }

    /** 
    * @Description: 根据dicType查询字典数据
    * @author yanggm 
    * @date 2018年9月14日 下午2:12:58 
    * @throws 
    */
    @PostMapping("get/{dicType}")
    public HandResp queryRedisByTypId(@PathVariable String dicType){
    	HandResp result = new HandResp();
	    List<BaseDictionary> list = baseDictionaryService.queryAllByType(dicType);
	    if(list!=null){
	    	result.setCode(Constant.SUCCESS);
	    	result.setList(list);
	    }else{
	    	result.setCode(Constant.FAIL);
	    	result.setMsg("该类型不存在");;
	    }
		return result;
    }

    /**
     * 获取用户登录工厂下的库位
     * @param param
     * @return
     */
    @PostMapping("getLocations")
    public HandResp getUserLocations(@RequestBody UserInfo param) {
        HandResp result = new HandResp();
//       1查询用户工厂
        String factoryCode = param.getFactoryCode();
//		2查询出工厂下的库位
        List<MdLocation> locations = new ArrayList<>();
        if (factoryCode!=null) {
            MdLocation mdLocation = new MdLocation();
            mdLocation.setFactoryCode(factoryCode);
            mdLocation.setActiveFlag("1");
            locations = locationService.findLocations(mdLocation);
            List<String> locs  = new ArrayList<>();
            for (MdLocation location : locations) {
                locs.add(location.getLocationCode());
            }
            result.setValues(locs);
            result.setCode(Constant.SUCCESS);
        }else {
            result.setCode(Constant.FAIL);
            result.setMsg("请输入工厂编码！");
        }

        return result;
    }
    
    /** 
	 * @Title queryContractList
	 * @Description 手持合同号下拉框
	 * @author luoteng
	 * @date 2018年6月6日:上午9:39:53
	 * @param mdContract
	 * @return HandResp
	 */
    @PostMapping("contract/select/get")
	public HandResp queryContractList(MdContract mdContract) {
		HandResp rtnResp = new HandResp();
		mdContract.setActiveFlag("1");
		List<MdContract> list = mdContractService.queryList(mdContract);
		rtnResp.setCode(Constant.SUCCESS);
		rtnResp.setList(list);
		return rtnResp;
	}

}
